package com.csy.property.community.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.csy.property.community.domain.AjBuilding;
import com.csy.property.community.domain.AjCommunity;
import com.csy.property.community.domain.dto.AjBuildingDto;
import com.csy.property.community.domain.dto.AjCommunityDto;
import com.csy.property.community.domain.vo.AjBuildingVo;
import com.csy.property.community.domain.vo.RoomTree;
import com.csy.property.community.mapper.AjBuildingMapper;
import com.csy.property.community.mapper.AjCommunityMapper;
import com.csy.property.community.service.AjBuildingService;
import com.csy.property.entity.AjCommunityArea;
import com.csy.property.mapper.AjCommunityAreaMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 楼栋实现类
 */
@Service
@Slf4j
public class AjBuildingServiceImpl implements AjBuildingService {

    @Resource
    private AjBuildingMapper ajBuildingMapper;

    @Resource
    private AjCommunityAreaMapper areaMapper;
    private final String CODE_PREFIX = "BUILDING_";
    @Resource
    AjCommunityMapper ajCommunityMapper;
    //查询楼栋列表
    @Override
    public List<AjBuilding> selectBuildingList(AjBuilding ajBuilding) {
        return ajBuildingMapper.selectBuildingList(ajBuilding);
    }

    /**
     * 新增
     * @param ajBuilding
     * @return
     */
    @Override
    public int insertAjBuilding(AjBuilding ajBuilding) {

        try {
            ajBuilding.setCommunityId(ajBuilding.getCommunityId());
        }catch (Exception e){
            log.error("小区id为空",e);
        }
        ajBuilding.setBuildingCode(CODE_PREFIX+System.currentTimeMillis());
        ajBuilding.setBuildingId(IdUtil.getSnowflake(1,1).nextId());
        Date date = new Date();
        ajBuilding.setCreateTime(date);
        return ajBuildingMapper.insert(ajBuilding);
    }

    /**
     * 查询小区信息
     * @param buildingId
     * @return
     */
    @Override
    public AjCommunity selectAjBuildingById(Long buildingId) {
        return null;
    }

    @Override
    public int updateAjBuilding(AjBuilding ajBuilding) {
        ajBuilding.setUpdateTime(new Date());
        return ajBuildingMapper.updateById(ajBuilding);
    }

    @Override
    public int deleteAjBuilding(Long[] buildingIds) {

        return ajBuildingMapper.deleteBatchIds(Arrays.asList(buildingIds));
    }

    @Override
    public List<AjBuilding> queryPullDown(Long communityId) {

        return ajBuildingMapper.queryPullDown(communityId);
    }

    @Override
    public AjBuilding selectById(Long buildingId) {
        return ajBuildingMapper.selectById(buildingId);
    }

    @Override
    public List<RoomTree> queryPullAreaBuilding(AjBuilding ajBuilding) {
        Long communityId = ajBuilding.getCommunityId();
        List<AjCommunityArea> areaList = areaMapper.getAreaList(new AjCommunityArea().setCommunityId(communityId));        //area集合
        List<RoomTree> areaBuildingTree = new ArrayList<>();
        if (areaList.size() == 0){
            //area对象
            RoomTree areaTree = new RoomTree();
            areaTree.setName("无");
            areaTree.setCode(0l);
            //building集合作为child集合
//            System.out.println("area为空-----");
            List<RoomTree> buildingtrees = new ArrayList<>();
            List<AjBuilding> list = ajBuildingMapper.queryPullDown(communityId);
            for (AjBuilding building:list){
                RoomTree roomTree = new RoomTree();
                roomTree.setCode(building.getBuildingId());
                roomTree.setName(building.getBuildingName());
                buildingtrees.add(roomTree);
            }
            areaTree.setChildren(buildingtrees);
            areaBuildingTree.add(areaTree);
        }else {
//            System.out.println("area不为空-----");
            for (AjCommunityArea ajCommunityArea: areaList){
                //area对象
                RoomTree areaTree = new RoomTree();
                areaTree.setName(ajCommunityArea.getCommunityArea());
                areaTree.setCode(ajCommunityArea.getId());
                //根据area对象areaId查building
                AjBuilding building = new AjBuilding();
                building.setCommunityAreaId(ajCommunityArea.getId());
                List<AjBuilding> ajBuildings = ajBuildingMapper.selectBuildingList(building);
                //building集合作为child集合
                List<RoomTree> buildingtrees = new ArrayList<>();
                for (AjBuilding ajBuild:ajBuildings){
                    //building对象
                    RoomTree buildTree = new RoomTree();
                    buildTree.setCode(ajBuild.getBuildingId());
                    buildTree.setName(ajBuild.getBuildingName());
                    buildingtrees.add(buildTree);
                }
                areaTree.setChildren(buildingtrees);
                areaBuildingTree.add(areaTree);
            }
        }
        return areaBuildingTree;
    }
}
